<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2012 HongPing All rights reserved.
// +----------------------------------------------------------------------
// | ActionName:数据备份还原模块
// +----------------------------------------------------------------------
// | Author: HongPing <hongping626@qq.com>
// +----------------------------------------------------------------------
class BacklogAction extends CommonAction {
	function _filter(&$map){
        //$map['id'] = array('egt',2);
        if($_POST['selectname']){
            $map['backurl'] = array('like',"%".$_POST['selectname']."%");
        }
        //dump(C ( 'DB_NAME' ));		
	}
    //数据库备份
    function  backData(){
        $dbname = C ( 'DB_NAME' );
        $dbuser = C ( 'DB_USER' );
        $dbpwd = C ( 'DB_PWD' );
      
        $filename=date("YmdHis").".sql"; 
        //$file = fopen($tmpFile, "w+"); 
        // 所保存的文件名   
       // header("Content-disposition:filename=".$filename);  
       // header("Content-type:application/octetstream");  
       // header("Pragma:no-cache");  
        //header("Expires:0");  
        // 获取当前页面文件路径，SQL文件就导出到此文件夹内   
        $tmpFile = '..\\Public\\backData\\'.$filename;  
        // 用MySQLDump命令导出数据库   
        exec("mysqldump -h localhost -u".$dbuser." -p".$dbpwd." --default-character-set=utf8 ".$dbname." > ".$tmpFile,$outStr,$oRes);  
        //echo $tmpFile;
        //$file=fopen($tmpFile,'r');
        //fwrite($file,$tmpFile);  
        ///echo fread($file,filesize($tmpFile));  
       // fclose($file);  
       // exit;
       if($oRes==0){
       $backM = D('Backlog');
       $backData['backurl'] = $filename;
       $backData['backtime'] = time();
       $backData['backuser'] = $_SESSION [C ( 'USER_AUTH_KEY' )];
       if($backM->add($backData)){
         $this->success('备份成功,备份文件为:'.$tmpFile);
       }else{
          unlink($tmpFile);
          $this->error('数据备份失败。');
       }
      
       }else{
        $this->error('备份失败。');
       }  
    }
    //删除备份点
    function deleteBack(){
        $backM = D('Backlog');
        $delMap['id'] = $_REQUEST['id'];
        $filename = $backM->where($delMap)->getField('backurl');
        $tmpFile = '..\\Public\\backData\\'.$filename;  
        
        if($backM->where($delMap)->delete()){
            unlink($tmpFile);
            $this->success('删除备份点成功:'.$tmpFile);
        }else{
            $this->error('删除失败');
        }
        
    }
    //还原数据
    function restoreBack(){
        $dbname = C ( 'DB_NAME' );
        $dbuser = C ( 'DB_USER' );
        $dbpwd = C ( 'DB_PWD' );
        $backM = D('Backlog');
        $relMap['id'] = $_REQUEST['id'];
        $filename = $backM->where($relMap)->getField('backurl');
        $thisIDData = $backM->select();
        $tmpFile = '..\\Public\\backData\\'.$filename;
        
        $result = mysql_query("SHOW tables");                       
        while ($currow=mysql_fetch_array($result))  
        {                       
           mysql_query("drop TABLE IF EXISTS $currow[0]");                      
                                
        }                       
        
        exec("mysql -u".$dbuser." -p".$dbpwd." ".$dbname." < ".$tmpFile,$outStr,$oRes); 
        if($oRes==0){
            //unlink($tmpFile);
            for($i=0;$i<count($thisIDData);$i++){
                $backM->add($thisIDData[$i]);
            }
            
            $backM->where($relMap)->setInc('rescount');
            $this->success('还原备份点成功:'.$tmpFile);
        }else{
            $this->success('还原备份点失败:'.$tmpFile);
        } 
        
    }
 
}
?>